package com.yuan.base.framework;

import android.app.Application;
import android.content.res.Configuration;

import com.yuan.base.utils.AppUtil;
import com.yuan.base.utils.FileUtil;
import com.yuan.base.utils.SDCardUtil;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;


/**
 * 基础application类(主工程需要继承，library工程不继承，直接使用)
 */
public class BaseAppContext extends Application {

	private static BaseAppContext instance = null;

	public static BaseAppContext getInstance() {
		if (instance == null) {
			instance = (BaseAppContext) AppUtil.INSTANCE;
		}
		return instance;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		instance = this;
		//xUtils工具类初始化
		x.Ext.init(this);
		// 捕捉异常
		BaseCrashHandler.getInstance().init();
	}

	public static String getSavePath() {
		String path = SDCardUtil.getSavePath(getInstance());
		FileUtil.createFilePath(path);
		return path;
	}

	public static String getSavePath(String str) {
		String path = SDCardUtil.getSavePath(getInstance()) + str;
		FileUtil.createFilePath(path);
		return path;
	}

	public static DbManager getDbUtils() throws DbException {
		if (BaseCommonInterface.getDbUtilsInterface() != null) {
			return BaseCommonInterface.getDbUtilsInterface().getDbUtil();
		}
		throw new DbException("未实现获取数据库接口");
	}


	@Override
	public void onTerminate() {
		super.onTerminate();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public void onLowMemory() {
		super.onLowMemory();
	}

}
